//
//  NSDictionary+propertyList.m
//  07_runtime(字典转模型KVC实现)
//
//  Created by afro on 15/12/26.
//  Copyright © 2015年 afro. All rights reserved.
//  用来生成字典中的key对应的属性列表

#import "NSDictionary+propertyList.h"

@implementation NSDictionary (propertyList)

- (void)getPropertyList {
    // 用来拼接code,只需要打印一次就够了
    NSMutableString *propertyList = [NSMutableString string];
    
    // 遍历字典中所有的key,根据key找到value,格式化输出属性列表
    [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
       
        // 打印Value的类型
        // NSLog(@"%@",[obj class]);
        // 判断value的真实类型
        NSString *code;
        if ([obj isKindOfClass:[NSString class]]) {
            code = [NSString stringWithFormat:@"@property(nonatomic, strong) NSString *%@;",key];
        } else if ([obj isKindOfClass:[NSArray class]]) {
            code = [NSString stringWithFormat:@"@property(nonatomic, strong) NSArray *%@;",key];
        } else if ([obj isKindOfClass:[NSDictionary class]]){
            code = [NSString stringWithFormat:@"@property(nonatomic, strong) NSDictionary *%@;",key];
        } else if ([obj isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){
            
            code = [NSString stringWithFormat:@"@property(nonatomic, assign) BOOL %@;",key];
        } else if ([obj isKindOfClass:[NSNumber class]]){
            code = [NSString stringWithFormat:@"@property(nonatomic, assign) NSInteger %@;",key];
        }
        
        [propertyList appendFormat:@"\n%@\n",code];
    }];
    
    // 打印输出生成的属性列表
    NSLog(@"%@",propertyList);
}
@end
